﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Krile.Kernel.WindowsEx;

namespace Twinkle.Dialogs.Account
{
    public partial class AccountsManager : Form
    {
        public AccountsManager()
        {
            InitializeComponent();
        }

        private void AccountsManager_Load(object sender, EventArgs e)
        {
            BuildList();
        }

        private void BuildList()
        {
            accountList.Items.Clear();
            if (Kernel.Config.Accounts != null)
            {
                foreach (var a in Kernel.Config.Accounts)
                {
                    accountList.Items.Add(a.ToString());
                }
            }
            accountList.SelectedIndex = -1;
        }

        bool rebuilding = false;
        private void accountList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rebuilding) return;
            rebuilding = true;
            int selIdx = accountList.SelectedIndex;
            BuildList();
            accountList.SelectedIndex = selIdx;
            rebuilding = false;
            if (accountList.SelectedIndex < 0 || accountList.SelectedIndex >= Kernel.Config.Accounts.Length)
            {
                detailBox.Enabled = false;
            }
            else
            {
                detailBox.Enabled = true;
                idLabel.Text = Kernel.Config.Accounts[accountList.SelectedIndex].UserId;
                recvCheck.Checked = (Kernel.Config.Accounts[accountList.SelectedIndex].UsingState & Twinkle.Data.Account.UsingStates.Receive) == Twinkle.Data.Account.UsingStates.Receive;
                updCheck.Checked = (Kernel.Config.Accounts[accountList.SelectedIndex].UsingState & Twinkle.Data.Account.UsingStates.Update) == Twinkle.Data.Account.UsingStates.Update;
                oprCheck.Checked = (Kernel.Config.Accounts[accountList.SelectedIndex].UsingState & Twinkle.Data.Account.UsingStates.Operate) == Twinkle.Data.Account.UsingStates.Operate;
            }
        }

        private void recvCheck_CheckedChanged(object sender, EventArgs e)
        {
            Kernel.Config.Accounts[accountList.SelectedIndex].UseForReceive = recvCheck.Checked;
        }

        private void updCheck_CheckedChanged(object sender, EventArgs e)
        {
            Kernel.Config.Accounts[accountList.SelectedIndex].UseForUpdate = updCheck.Checked;
        }

        private void oprCheck_CheckedChanged(object sender, EventArgs e)
        {
            Kernel.Config.Accounts[accountList.SelectedIndex].UseForOperate = oprCheck.Checked;
        }

        private void accEditBtn_Click(object sender, EventArgs e)
        {
            using(var ac = new Dialogs.Account.AccountEditor(Kernel.Config.Accounts[accountList.SelectedIndex]))
            {
                if (ac.ShowDialog() == DialogResult.OK)
                {
                    accountList_SelectedIndexChanged(null, null);
                }
            }
        }

        private void accDeleteBtn_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show(Lang.Msg.DeleteAccount, Lang.Msg.DeleteAccount_Title,
                MessageBoxEx.MessageBoxExButtons.YesNo, MessageBoxEx.MessageBoxExIcon.Warning
                ) == MessageBoxEx.DialogResultEx.Yes)
            {
                List<Data.Account> acs = new List<Twinkle.Data.Account>();
                foreach (var a in Kernel.Config.Accounts)
                {
                    if (a != Kernel.Config.Accounts[accountList.SelectedIndex])
                        acs.Add(a);
                }
                Kernel.Config.Accounts = acs.ToArray();
                BuildList();
            }
        }

        private void addAccountBtn_Click(object sender, EventArgs e)
        {
            Data.Account a = new Twinkle.Data.Account();
            using (var ac = new Dialogs.Account.AccountEditor(a))
            {
                if (ac.ShowDialog() == DialogResult.OK)
                {
                    List<Data.Account> acs = new List<Twinkle.Data.Account>();
                    if (Kernel.Config.Accounts != null)
                        acs.AddRange(Kernel.Config.Accounts);
                    acs.Add(a);
                    Kernel.Config.Accounts = acs.ToArray();
                    accountList_SelectedIndexChanged(null, null);
                }
            }
        }

        private void AccountsManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            Kernel.Config.SaveConfig();
        }
    }
}
